home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / dejagnu.lha / dejagnu-1.0.1 / expect / exp_log.c < prev    next >
C/C++ Source or Header  |  1993-04-15  |  6KB  |  248 lines

  1. /* exp_log.c - logging routines */
  2.  
  3. #include "exp_conf.h"
  4. #include <stdio.h>
  5. #include <varargs.h>
  6. #include "exp_global.h"
  7. #include "exp_rename.h"
  8. #include "exp_log.h"
  9.  
  10. int loguser = TRUE;        /* if TRUE, expect/spawn may write to stdout */
  11. int logfile_all = FALSE;    /* if TRUE, write log of all interactions */
  12.                 /* despite value of loguser. */
  13. FILE *logfile = 0;
  14. FILE *cmdfile = 0;
  15. FILE *debugfile = 0;
  16. int exp_is_debugging = FALSE;
  17.  
  18. /* Following this are several functions that log the conversation. */
  19. /* Most of them have multiple calls to printf-style functions.  */
  20. /* At first glance, it seems stupid to reformat the same arguments again */
  21. /* but we have no way of telling how long the formatted output will be */
  22. /* and hence cannot allocate a buffer to do so. */
  23. /* Fortunately, in production code, most of the duplicate reformatting */
  24. /* will be skipped, since it is due to handling errors and debugging. */
  25.  
  26. /* send to log if open */
  27. /* send to stderr if debugging enabled */
  28. /* use this for logging everything but the parent/child conversation */
  29. /* (this turns out to be almost nothing) */
  30. /* uppercase L differentiates if from math function of same name */
  31. #define LOGUSER        (loguser || force_stdout)
  32. /*VARARGS*/
  33. void
  34. Log(va_alist)
  35. va_dcl
  36. {
  37.     int force_stdout;
  38.     char *fmt;
  39.     va_list args;
  40.  
  41.     va_start(args);
  42.     force_stdout = va_arg(args,int);
  43.     fmt = va_arg(args,char *);
  44.     if (debugfile) vfprintf(debugfile,fmt,args);
  45.     if (logfile_all || (LOGUSER && logfile)) vfprintf(logfile,fmt,args);
  46.     if (LOGUSER) vfprintf(stdout,fmt,args);
  47.     va_end(args);
  48. }
  49.  
  50. /* just like log but does no formatting */
  51. /* send to log if open */
  52. /* use this function for logging the parent/child conversation */
  53. void
  54. nflog(buf,force_stdout)
  55. char *buf;
  56. int force_stdout;    /* override value of loguser */
  57. {
  58.     int length = strlen(buf);
  59.  
  60.     if (debugfile) fwrite(buf,1,length,debugfile);
  61.     if (logfile_all || (LOGUSER && logfile)) fwrite(buf,1,length,logfile);
  62.     if (LOGUSER) fwrite(buf,1,length,stdout);
  63. #if 0
  64.     if (logfile_all || (LOGUSER && logfile)) {
  65.         int newlength = exp_copy_out(length);
  66.         fwrite(exp_out_buffer,1,newlength,logfile);
  67. }
  68. #endif
  69. }
  70. #undef LOGUSER
  71.  
  72. /* send to log if open and debugging enabled */
  73. /* send to stderr if debugging enabled */
  74. /* use this function for recording unusual things in the log */
  75. /*VARARGS*/
  76. void
  77. debuglog(va_alist)
  78. va_dcl
  79. {
  80.     char *fmt;
  81.     va_list args;
  82.  
  83.     va_start(args);
  84.     fmt = va_arg(args,char *);
  85.     if (debugfile) vfprintf(debugfile,fmt,args);
  86.     if (is_debugging) {
  87.         vfprintf(stderr,fmt,args);
  88.         if (logfile) vfprintf(logfile,fmt,args);
  89.     }
  90.  
  91.     va_end(args);
  92. }
  93.  
  94. /* send to log if open */
  95. /* send to stderr */
  96. /* use this function for error conditions */
  97. /*VARARGS*/
  98. void
  99. errorlog(va_alist)
  100. va_dcl
  101. {
  102.     char *fmt;
  103.     va_list args;
  104.  
  105.     va_start(args);
  106.     fmt = va_arg(args,char *);
  107.     vfprintf(stderr,fmt,args);
  108.     if (debugfile) vfprintf(debugfile,fmt,args);
  109.     if (logfile) vfprintf(logfile,fmt,args);
  110.     va_end(args);
  111. }
  112.  
  113. /* just like errorlog but does no formatting */
  114. /* send to log if open */
  115. /* use this function for logging the parent/child conversation */
  116. /*ARGSUSED*/
  117. void
  118. nferrorlog(buf,force_stdout)
  119. char *buf;
  120. int force_stdout;    /* not used, only declared here for compat with */
  121.             /* nflog() */
  122. {
  123.     int length = strlen(buf);
  124.     fwrite(buf,1,length,stderr);
  125.     if (debugfile) fwrite(buf,1,length,debugfile);
  126.     if (logfile) fwrite(buf,1,length,logfile);
  127. }
  128.  
  129. #if 0
  130. static int out_buffer_size;
  131. static char *outp_last;
  132. static char *out_buffer;
  133. static char *outp;    /* pointer into out_buffer - static in order */
  134.             /* to update whenever out_buffer is enlarged */
  135.  
  136.  
  137. void
  138. exp_init_log()
  139. {
  140.     out_buffer = malloc(BUFSIZ);
  141.     out_buffer_size = BUFSIZ;
  142.     outp_last = out_buffer + BUFSIZ - 1;
  143. }
  144.  
  145. char *
  146. enlarge_out_buffer()
  147. {
  148.     int offset = outp - out_buffer;
  149.  
  150.     int new_out_buffer_size = out_buffer_size = BUFSIZ;
  151.     if (0 == realloc(out_buffer,new_out_buffer_size)) return(0);
  152.  
  153.     out_buffer_size = new_out_buffer_size;
  154.     outp = out_buffer + offset;
  155.  
  156.     outp_last = out_buffer + out_buffer_size - 1;
  157.  
  158.     return(out_buffer);
  159. }
  160.  
  161. /* like sprintf, but uses a static buffer enlarged as necessary */
  162. /* currently supported are %s, %d, and %#d where # is a single-digit */
  163. void
  164. exp_sprintf(va_alist)
  165. va_dcl
  166. {
  167.     char *fmt;
  168.     va_list args;
  169.     char int_literal[20];    /* big enough for an int literal? */
  170.     char *int_litp;        /* pointer into int_literal */
  171.     char *width;
  172.     char *string_arg;
  173.     int int_arg;
  174.     char *int_fmt;
  175.  
  176.     va_start(args);
  177.     fmt = va_arg(args,char *);
  178.  
  179.     while (*fmt != '\0') {
  180.         if (*fmt != '%') {
  181.             *outp++ = *fmt++;
  182.             continue;
  183.         }
  184.  
  185.         /* currently, only single-digit widths are used */
  186.         if (isdigit(*fmt)) {
  187.             width = fmt++;
  188.         } else width = 0;
  189.  
  190.         switch (*fmt) {
  191.         case 's':    /* interpolate string */
  192.             string_arg = va_arg(args,char *);
  193.  
  194.             while (*string_arg) {
  195.                 if (outp == outp_last) {
  196.                     if (enlarge_out_buffer() == 0) {
  197.                         /* FAIL */
  198.                         return;
  199.                     }
  200.                 }
  201.                 *outp++ = *string_arg++;
  202.             }
  203.             fmt++;
  204.             break;
  205.         case 'd':    /* interpolate int */
  206.             int_arg = va_arg(args,int);
  207.  
  208.             if (width) int_fmt = width;
  209.             else int_fmt = fmt;
  210.  
  211.             sprintf(int_literal,int_fmt,int_arg);
  212.  
  213.             int_litp = int_literal;
  214.             for (int_litp;*int_litp;) {
  215.                 if (enlarge_out_buffer() == 0) return;
  216.                 *outp++ = *int_litp++;
  217.             }
  218.             fmt++;
  219.             break;
  220.         default:    /* anything else is literal */
  221.             if (enlarge_out_buffer() == 0) return;    /* FAIL */
  222.             *outp++ = *fmt++;
  223.             break;
  224.         }
  225.     }
  226. }
  227.  
  228. /* copy input string to exp_output, replacing \r\n sequences by \n */
  229. /* return length of new string */
  230. int
  231. exp_copy_out(char *s)
  232. {
  233.     outp = out_buffer;
  234.     int count = 0;
  235.  
  236.     while (*s) {
  237.         if ((*s == '\r') && (*(s+1) =='\n')) s++;
  238.         if (enlarge_out_buffer() == 0) {
  239.             /* FAIL */
  240.             break;
  241.         }
  242.         *outp = *s;
  243.         count++;
  244.     }
  245.     return count;
  246. }
  247. #endif
  248.